﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Booking_Hotel.BLL;
using Booking_Hotel.DBML;
using System.Web.Security;
using Booking_Hotel.DAL;

namespace Booking_Hotel
{
    public partial class Supplier_LoaiPhong : System.Web.UI.Page
    {
        Supplier_Hotel_LoaiPhong entities = new Supplier_Hotel_LoaiPhong();
        ThanhBHotelDataContext tbk = new ThanhBHotelDataContext();
      
       // int Supplier_Hotel_LoaiPhongID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["ss_SupplierID"] = 2;//Gan SupplierID cho Dropdownlist de lay DS khach san cua Supplier
                ShowLoaiPhong();
            }
        }
        protected void ShowLoaiPhong()
        {
            var loaiphong = from dv in tbk.Supplier_Hotels
                         join shl in tbk.Supplier_Hotel_LoaiPhongs on dv.Supplier_HotelID equals shl.Supplier_HotelID
                         where dv.SupplierID == (int)Session["ss_SupplierID"]
                         select new
                         {
                             Supplier_Hotel_LoaiPhongID= shl.Supplier_Hotel_LoaiPhongID,
                             Supplier_HotelID = shl.Supplier_HotelID,
                             TenKhachSan = dv.TenKhachSan,
                             TenLoaiPhong = shl.TenLoaiPhong,
                             GiaPhong= shl.GiaPhong,
                             Description = shl.Description,
                             TrangThaiLoaiPhong = shl.TrangThaiLoaiPhong    
                         };
            GridView1.DataSource = loaiphong;
            GridView1.DataBind();

        }


        protected void Bt_Insert_Click(object sender, EventArgs e)
        {
            try
            {
                entities = new Supplier_Hotel_LoaiPhong();
                entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                entities.TenLoaiPhong = txt_TenLoaiPhong.Text.Trim();
                entities.GiaPhong = Decimal.Parse(txt_GiaPhong.Text.Trim());
                entities.Description = txt_Desciption.Text.Trim();
                entities.TrangThaiLoaiPhong = CB_TrangThaiPhong.Checked;
                Supplier_LoaiPhongDAO sl = new Supplier_LoaiPhongDAO();
                sl.addSupplier_Hotel_LoaiPhong(entities);
                ShowLoaiPhong();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow select = GridView1.Rows[e.NewSelectedIndex];
            Session["ss_Supplier_Hotel_LoaiPhongID"] = int.Parse(select.Cells[0].Text.Trim());
            DDL_TenKhachSan.SelectedValue = select.Cells[1].Text.Trim();
            txt_TenLoaiPhong.Text = select.Cells[3].Text.Trim();
            txt_GiaPhong.Text = select.Cells[4].Text.Trim();
            txt_Desciption.Text = select.Cells[5].Text.Trim();
            CB_TrangThaiPhong.Checked=((CheckBox)select.Cells[6].Controls[0]).Checked;
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 0)
            {
                e.Row.Cells[0].Visible = false;
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void Bt_CapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                int check_Supplier_Hotel_LoaiPhongID = (int)(Session["ss_Supplier_Hotel_LoaiPhongID"]);
                if (check_Supplier_Hotel_LoaiPhongID != null)
                {
                    entities = new Supplier_Hotel_LoaiPhong();
                    entities.Supplier_Hotel_LoaiPhongID = check_Supplier_Hotel_LoaiPhongID;
                    entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                    entities.TenLoaiPhong = txt_TenLoaiPhong.Text.Trim();
                    entities.GiaPhong = Decimal.Parse(txt_GiaPhong.Text.Trim());
                    entities.Description = txt_Desciption.Text.Trim();
                    entities.TrangThaiLoaiPhong = CB_TrangThaiPhong.Checked;
                    Supplier_LoaiPhongDAO sd = new Supplier_LoaiPhongDAO();
                    sd.updateSupplier_Hotel_LoaiPhong(entities);
                    ShowLoaiPhong();
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Vui lòng chọn đối tượng cần update dưới lưới", this.Page);

                }

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}